Content & Location User Control (Flexible Usage)

SearchBoxOptions is simple to use, but there are many ways that these options can be presented, and many possible requirements for operational logic that can be applied. The following is a User Control (presented in both languages in the demo project) that shows the necessary requirements for integration with the SearchBox control (basically the only requirement is that SearchOptions property in SearchBox is set when the User Control loads).

Example Code (ASCX)

<%@ Control Language="c#" AutoEventWireup="false"
Codebehind="SearchOptionsUC.ascx.cs" Inherits="Test.SearchOptionsUC"
TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>

<asp:Label id="Label1" runat="server" Font-Bold="True">Search which location?</
asp:Label>

<asp:radiobuttonlist id="RadioButtonList1" runat="server"
RepeatDirection="Horizontal">
    <asp:ListItem Value="All" Selected="True">All</asp:ListItem>
    <asp:ListItem Value="Community">Community</asp:ListItem>
</asp:radiobuttonlist>

<asp:Label id="Label2" runat="server" Font-Bold="True">In which categories?</
asp:Label>

<asp:checkboxlist id="CheckBoxList1" runat="server">
    <asp:ListItem Value="Books" Selected="True">Books</asp:ListItem>
    <asp:ListItem Value="Fiction">Fiction</asp:ListItem>
    <asp:ListItem Value="Nonfiction">Nonfiction</asp:ListItem>
    <asp:ListItem Value="Stationary" Selected="True">Stationary</asp:ListItem>
    <asp:ListItem Value="Messages">Messages</asp:ListItem>
</asp:checkboxlist>

This code simply presents the user with options for choosing location and content categories; note that names of existing content and location categories from the index have been used.

Example Code (ASCX Codebehind)

namespace Test
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Collections;
    using System.ComponentModel;
    using System.Web.UI;
    using Keyoti.SearchEngine.Search;
    using Keyoti.SearchEngine.Web;
    public class SearchOptionsUC : System.Web.UI.UserControl
    {
        string location;
        string[] contents;

        protected System.Web.UI.WebControls.RadioButtonList RadioButtonList1;
        protected System.Web.UI.WebControls.Label Label1;
        protected System.Web.UI.WebControls.Label Label2;
        protected System.Web.UI.WebControls.CheckBoxList CheckBoxList1;

        private void Page_Load(object sender, System.EventArgs e)
        {   

            //find the SearchBox control
            Control c = FindRecursive( Page.Controls );
            //if we couldn't find one, throw exception
            if( c==null )
                throw new Exception("A SearchBox Control "+
                "could not be found on the Form, please add one.");
            else
            {
                //if SearchBox found, set its SearchOptions
                //accordingly with the location/content from
                //the controls
                location = RadioButtonList1.SelectedItem.ToString();

                  //need to pass an array of strings to SearchBox
                //for the content options.
                //So find selected contents and convert to an array
                  ArrayList temporary = new ArrayList();
                  foreach( ListItem item in CheckBoxList1.Items)
                      if( item.Selected )
                        temporary.Add(item.Value);

                  contents = (string[]) temporary.ToArray(typeof
(System.String));

                  //finally set SearchOption property in SearchBox
                ((SearchBox)c).SearchOptions = new
SearchOptions(location, contents);
                  }
            }

        /// <summary>
        /// Finds a SearchBox control in controlsToFindIn
       /// </summary>
        Control FindRecursive(ControlCollection controlsToFindIn)
        {
            Control found = null;
            foreach(Control crt in controlsToFindIn)
            {
                if (found==null)
                {
                    if(crt is Keyoti.SearchEngine.Web.SearchBox)
                        found = crt;
                    else found = FindRecursive(crt.Controls);
                }
            }
            return found;
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.Load += new System.EventHandler(this.Page_Load);
        }
        #endregion
    }
}

The Page_Load method of the User Control is where the required action is performed; The SearchBox.SearchOptions property is set with the exact names of content and location categories as specified in the index.